package com.mayisoftware.mayioa.business.score.impl.controller;

import com.mayisoftware.mayioa.business.pool.api.service.IPoolManualService;
import com.mayisoftware.mayioa.business.score.api.domain.ScoreInfo;
import com.mayisoftware.mayioa.business.score.api.service.IScoreInfoService;
import com.mayisoftware.mayioa.common.api.annotation.Log;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.api.enums.BusinessType;
import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.api.domain.SysMenu;
import com.mayisoftware.mayioa.platform.api.domain.SysPost;
import com.mayisoftware.mayioa.platform.api.domain.SysUser;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/score/score")
public class ScoreInfoController extends BaseController {
    private String prefix = "score/score";

    @Autowired
    IScoreInfoService scoreInfoService;

    @Autowired
    IPoolManualService poolManualService;

    @RequiresPermissions("score:score:view")
    @GetMapping()
    public String scoreInfo(ModelMap mmap)
    {
        mmap.put("planList",scoreInfoService.getPlanSelectList(ShiroUtils.getUserId()));
        return prefix + "/score";
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{scoreRecordId}")
    public String edit(@PathVariable("scoreRecordId") Long recordId, ModelMap mmap)
    {
        mmap.put("planList",scoreInfoService.getPlanSelectList(ShiroUtils.getUserId()));
        mmap.put("scoreInfo",scoreInfoService.selectByPrimaryKey(recordId) );
        return prefix + "/edit";
    }


    /**
     * 修改保存用户
     */
    @RequiresPermissions("score:score:edit")
    @Log(title = "打分管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(ScoreInfo scoreInfo)
    {

        scoreInfo.setScoreUpdateUserId(ShiroUtils.getUserId().intValue());
        scoreInfo.setScoreUpdateTime(new Date());
        int rows = scoreInfoService.updateScoreInfo(scoreInfo);
        poolManualService.modifyPoolInfoToCacl();
        return toAjax(rows);
    }

    /**
     * 打分页
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("planList",scoreInfoService.getPlanSelectList(ShiroUtils.getUserId()));
        return prefix + "/add";
    }


    /**
     * 打分
     */
    @Log(title = "打分管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("score:score:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ScoreInfo scoreInfo)
    {
        scoreInfo.setScoreUpdateTime(new Date());
        scoreInfo.setScoreUpdateUserId(ShiroUtils.getUserId().intValue());

        int s = scoreInfoService.insertScoInfo(scoreInfo);
        if(s==-1){
            return error("该项目已经打分！");
        }
        poolManualService.modifyPoolInfoToCacl();
        return toAjax(s);
    }

    @RequiresPermissions("score:score:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScoreInfo scoreInfo)
    {
        startPage();
        List<ScoreInfo> list = scoreInfoService.selectScoreInfoList(scoreInfo,ShiroUtils.getUserId());
        return getDataTable(list);
    }

    /**
     * ajax 查询参加当前计划的用户
     * @param scorePlanId
     * @return
     */
    @GetMapping("/getSignPlanUsers")
    @ResponseBody
    public List<Map<String,Object>> getSignPlanUsers(Long scorePlanId){
        if(scorePlanId==null){
            return new ArrayList<Map<String,Object>>();
        }
        return scoreInfoService.getSignPlanUsers(scorePlanId);
    }

    /**
     * ajax查询当前计划明细下拉列表
     * @param scorePlanId
     * @return
     */
    @GetMapping("/getPlanDetailSelect")
    @ResponseBody
    public List<Map<String,Object>> getPlanDetailSelect(Long scorePlanId){
        if(scorePlanId==null){
            return new ArrayList<Map<String,Object>>();
        }
        return scoreInfoService.selectPlanDetails(scorePlanId);
    }

    /**
     * ajax查询当前计划明细下拉列表
     * @param scorePlanId
     * @return
     */
    @GetMapping("/getPlanDetailScoreSelect")
    @ResponseBody
    public List<Map<String,Object>> selectPlanDetailsScore(@Param("scorePlanId") Long scorePlanId,@Param("userId") Long userId){
        if(scorePlanId==null){
            return new ArrayList<Map<String,Object>>();
        }
        return scoreInfoService.selectPlanDetailsScore(scorePlanId,userId);
    }



    @RequiresPermissions("score:score:remove")
    @Log(title = "打分管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            int rows = scoreInfoService.deleteScoreInfoByIds(ids);
            poolManualService.modifyPoolInfoToCacl();
            return toAjax(rows);
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

}